//
//  SearchViewController.m
//  ProductB
//
//  Created by 顾佳辉 on 16/6/1.
//  Copyright © 2016年 ChenZheng. All rights reserved.
//

#import "SearchViewController.h"
#import "UIBarButtonItem+WNXBarButtonItem.h"
#import "searchModel.h"
#import "SearchCell.h"


@interface SearchViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,UITextViewDelegate,UISearchBarDelegate>

//searchbar
@property (nonatomic, strong) UISearchBar  *searchbar;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UITextView *textView;



@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, kWidth - 100, 25)];
    self.searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, kWidth - 100, 25)];
    self.searchbar.delegate = self;
    self.searchbar.layer.cornerRadius = 10;
    self.searchbar.placeholder = @"搜索";
    self.searchbar.layer.masksToBounds = YES;
    self.searchbar.layer.borderWidth = 2;
    self.searchbar.layer.borderColor = [UIColor whiteColor].CGColor;
    self.searchbar.delegate = self;
    [titleView addSubview:self.searchbar];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:titleView];

    [self setNavigationItem];
    [self settableView];
    
}

    
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
  [self requestNetWorkstartURLText:[searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
    

    


- (void)settableView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"SearchCell" bundle:nil] forCellReuseIdentifier:@"Search"];
    [self.view addSubview:self.tableView];
    
}

- (void)setNavigationItem
{

    
    //取消按钮
    UIBarButtonItem *rightItem = [UIBarButtonItem initWithTitle:@"取消" titleColor: [UIColor colorWithRed:213/255.0 green:213/255.0 blue:213/255.0 alpha:1] target:self action:@selector(backClick)];
    self.navigationItem.rightBarButtonItem = rightItem;
}


- (void)backClick
{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.searchbar becomeFirstResponder];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.searchbar resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//网络请求
- (void)requestNetWorkstartURLText:(NSString *)text
{
    
    NSString *string =[NSString stringWithFormat:@"http://q.chanyouji.com/api/v1/districts/search.json?latitude=0&longitude=0&name=%@",text];
    [RequestManager initWithUrlString:string requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        self.dataArray = [NSMutableArray array];
            [self.dataArray removeAllObjects];
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dataDic);
        NSArray *activity = dataDic[@"data"];
        for (NSDictionary *dic in activity) {
            searchModel *model = [[searchModel alloc]init];
            model.name = dic[@"name"];
            [self.dataArray addObject:model];
               }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
     
    } errorBlock:^(NSError *error) {
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Search" forIndexPath:indexPath];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    searchModel *model = self.dataArray[indexPath.row];
    if (self.blockName) {
        self.blockName(model.name);
    }
    [self.navigationController popViewControllerAnimated:YES];

}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
